/**
 * 系统管理 用户管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <!-- <el-form-item label="搜索：">
        <el-select size="small" v-model="formInline.isLock" placeholder="请选择">
          <el-option label="全部" value=""></el-option>
          <el-option label="正常" value="N"></el-option>
          <el-option label="已锁定" value="Y"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="">
        <el-input
          size="small"
          v-model="formInline.username"
          placeholder="输入微信昵称"
        ></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input
          size="small"
          v-model="formInline.consignee"
          placeholder="输入收货人"
        ></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input
          size="small"
          v-model="formInline.mobile"
          placeholder="输入收货人手机号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <!-- <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
        <el-button size="small" type="primary" @click="handleunit()">部门设置</el-button> -->
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="userData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <!-- <el-table-column align="center" type="selection" width="50">
      </el-table-column> -->
      <el-table-column
        align="center"
        sortable
        prop="username"
        label="微信昵称"
        width="200"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="avatarUrl"
        label="头像"
        width="200"
      >
        <template slot-scope="scope">
          <img :src="scope.row.avatarUrl" style="height: 50px" />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="gender"
        label="性别"
        width="200"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="isEnabled"
        label="是否启用"
        min-width="200"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isEnabled == 1 ? nshow : fshow"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="editType(scope.$index, scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="地址栏" min-width="300" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >查看地址库</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <!-- 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="90%"
      @click="closeDialog('edit')"
    >
      <el-table
        size="small"
        :data="addressList"
        highlight-current-row
        v-loading="loading"
        border
        element-loading-text="拼命加载中"
        style="width: 100%"
      >
        <el-table-column
          align="center"
          prop="consignee"
          label="收货人"
          width="200"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="mobile"
          label="联系电话"
          width="200"
        >
        </el-table-column>
        <el-table-column align="center" prop="address" label="收货地址">
        </el-table-column>
        <el-table-column
          align="center"
          prop="isDefault"
          label="是否为默认地址"
          width="130"
        >
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button size="small" @click="closeDialog('edit')">关闭</el-button>
        <!-- <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')"></el-button> -->
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入请求方法
import Pagination from "../../components/Pagination";
import { userList, userUpdate, addressList } from "../../api/userMG";
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      title: "查看地址库",
      editFormVisible: false, //控制编辑页面显示与隐藏
      dataAccessshow: false, //控制数据权限显示与隐藏
      unitAccessshow: false, //控制所属单位隐藏与显示
      // 编辑与添加
      editForm: {
        userId: "",
        userName: "",
        userRealName: "",
        roleId: "",
        userMobile: "",
        userEmail: "",
        userSex: "",
        token: localStorage.getItem("logintoken"),
      },
      // 部门参数
      // unitparm: {
      //   userIds: '',
      //   deptId: '',
      //   deptName: ''
      // },
      // 选择数据
      // selectdata: [],
      // rules表单验证
      // rules: {
      //   userName: [
      //     { required: true, message: '请输入用户名', trigger: 'blur' }
      //   ],
      //   userRealName: [
      //     { required: true, message: '请输入姓名', trigger: 'blur' }
      //   ],
      //   roleId: [{ required: true, message: '请选择角色', trigger: 'blur' }],
      //   userMobile: [
      //     { required: true, message: '请输入手机号', trigger: 'blur' },
      //     {
      //       pattern: /^1(3\d|47|5((?!4)\d)|7(0|1|[6-8])|8\d)\d{8,8}$/,
      //       required: true,
      //       message: '请输入正确的手机号',
      //       trigger: 'blur'
      //     }
      //   ],
      //   userEmail: [
      //     { required: true, message: '请输入邮箱', trigger: 'blur' },
      //     {
      //       pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
      //       required: true,
      //       message: '请输入正确的邮箱',
      //       trigger: 'blur'
      //     }
      //   ],
      //   userSex: [{ required: true, message: '请选择性别', trigger: 'blur' }]
      // },
      // // 删除用户
      // seletedata: {
      //   ids: '',
      //   token: localStorage.getItem('logintoken')
      // },
      // // 重置密码
      // resetpsd: {
      //   userId: '',
      //   token: localStorage.getItem('logintoken')
      // },
      // // 用户下线
      // offline: {
      //   token: localStorage.getItem('logintoken')
      // },
      // 请求数据参数
      formInline: {
        size: 1,
        current: 10,
      },
      //用户数据
      userData: [],
      addressList: [],
      // 数据权限
      // UserDept: [],
      // defaultProps: {
      //   children: 'children',
      //   label: 'name'
      // },
      // 选中
      // checkmenu: [],
      //参数role
      // saveroleId: '',
      // 分页参数
      pageparm: {
        current: 1,
        size: 10,
        total: 10,
      },
    };
  },
  // 注册组件
  components: {
    Pagination,
  },

  /**
   * 数据发生改变
   */
  watch: {},

  /**
   * 创建完毕
   */
  created() {
    this.formInline.current = 1;
    this.formInline.size = 10;
    let data = {
      pageVo: this.formInline,
      username: "",
      consignee: "",
      mobile: "",
    };
    this.getdata(data);
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取数据方法
    async getdata(parameter) {
      this.loading = true;
      var that = this;
      // 获取用户列表
      await userList(parameter).then((res) => {
        if (res.code == "200") {
          that.loading = false;
          that.userData = res.data.records;
          that.pageparm.current = res.data.current;
          that.pageparm.size = res.data.size;
          that.pageparm.total = res.data.total;
        } else {
          // that.loading = true
          that.userData = [];
        }
      });

      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
      // 获取用户列表
      // userList(parameter).then(res => {
      //   this.loading = false
      //   if (res.success == false) {
      //     this.$message({
      //       type: 'info',
      //       message: res.msg
      //     })
      //   } else {
      //     this.userData = res.data
      //     // 分页赋值
      //     this.pageparm.currentPage = this.formInline.page
      //     this.pageparm.pageSize = this.formInline.limit
      //     this.pageparm.total = res.count
      //   }
      // })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      let data = {
        pageVo: this.formInline,
        username: this.formInline.username,
        consignee: this.formInline.consignee,
        mobile: this.formInline.mobile,
      };
      this.getdata(data);
    },
    //搜索事件
    search() {
      let data = {
        pageVo: this.formInline,
        username: this.formInline.username,
        consignee: this.formInline.consignee,
        mobile: this.formInline.mobile,
      };
      this.getdata(data);
    },
    // 修改type
    editType: function (index, row) {
      // updateIsEnable
      this.loading = true;
      let userUpdateVo = {
        userId: row.id,
        isEnable: row.isEnabled == 1 ? 0 : 1,
      };
      // 修改状态
      userUpdate(userUpdateVo).then((res) => {
        this.loading = false;
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "状态修改成功",
          });
          let data = {
            pageVo: this.formInline,
            username: this.formInline.username,
            consignee: this.formInline.consignee,
            mobile: this.formInline.mobile,
          };
          this.getdata(data);
        } else {
          this.$message({
            type: "info",
            message: res.msg,
          });
        }
      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.addressList = [];
      this.editFormVisible = true;
      // 获取地址列表信息
      addressList(row.id).then((res) => {
        if (res.code == 200) {
          this.addressList = res.data;
        }
      });
    },

    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == "edit") {
        this.editFormVisible = false;
      } else if (dialog == "perm") {
        this.dataAccessshow = false;
      } else if (dialog == "unit") {
        this.unitAccessshow = false;
      }
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 